<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>实验3</title>
</head>
<body>
<script>
    //189000950 顾啟思
    //1
    var obj1 ={
        name: "刘备",
        sex: "男",
        run: function() {
            console.log("跑");
        }
    }
    //2 使用object方法创建
    var obj2 = new Object();
    obj2.name = "刘备";
    obj2.sex = "男";
    obj2.run = function(){
        console.log("跑");
    }
    //3 使用工厂模式创建
    function createPerson(p_name, p_sex, p_run) {
        var temPerson = new Object();
        temPerson.name = p_name;
        temPerson.sex = p_sex;
        temPerson.run = p_run;
        return temPerson;
    }
    var obj3 = createPerson("刘备", "男", new Function("console.log('跑')"));
    //4
    function Student(name, sex){
        this.name = name;
        this.sex = sex;
        this.run = function(){
            console.log(this.name + "跑");
        }
    }
    var obj5 = new Student("刘备", "男");
    //5
    function Person() {        
    }
    Person.prototype.name = "刘备";
    Person.prototype.sex = "男";
    Person.prototype.run = function() {
        console.log(this.name + "跑");
    };
    var obj6 = new Person();
    console.log(obj6);
    obj6.run();
    //6
    function fun(){
        console.log(this);//this指向Window 
        //原因：在这个上下文（执行环境）函数并没有绑定到任何一个对象中，意味着 this 指向 window
    }
    fun();
    //7 Dog继承Ainimal
    function Ainimal(name){
        this.name = name;
    }
    function Dog(name,price){
        Ainimal.call(this,name);//对象冒充
        this.price = price;
    }
    var obj7 = new Dog("拉布拉多",9.9);
    //8 组合继承(伪经典继承)
    function Person(name){
        this.name = name;
    }
    Person.prototype.run = function(){
        console.log("跑")
    }

    function Worker(name,salary){
        Person.call(this, name);
        this.salary = salary;
    }
    Worker.prototype = new Person();//利用原型获取方法
    var obj4 = new Worker("赵云", 100);
    console.log(obj4);
    //9 原型式继承
    var person1 = {
        name: "刘备",
        sex: "男",
        run:function(){
            console.log("跑");
        }
    }
    var person2 = Object.create(person1);
    console.log(person2);
    //10 寄生组合式继承
    function inheritPrototype(sFun,pFun){
        var myPrototype = new Object(pFun.prototype);
        myPrototype.constructor = sFun;
        sFun.prototype = myPrototype;
    }

    function Person(name){
        this.name = name;
    }
    Person.prototype.run = function(){
        console.log("跑");
    }

    function Worker(name, salary){
        Person.call(this,name);
        this.salary = salary;
    }

    inheritPrototype(Worker,Person);

    var worker1 = new Worker("赵云",100);
    console.log(worker1);
</script>    
</body>
</html>